//
//  JSTabBarController.m
//  jshop
//
//  Created by 张艳江 on 2019/4/29.
//  Copyright © 2019年 张艳江. All rights reserved.
//

#import "SCTabBarController.h"
#import "SCTabBar.h"
#import "SCNavigationController.h"
#import "SCHomeController.h"
#import "SCIntegralMallController.h"
#import "SCShopCartController.h"
#import "SCMineController.h"

@interface SCTabBarController ()<UITabBarControllerDelegate>

@end

@implementation SCTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    // 创建自定义tabbar
    [self addCustomTabBar];
    // 设置子控制器
    [self setUpChildViewControllers];
    // 设置tabBar的一些属性
    [self setupTabBarItem];
}
#pragma mark - 设置子控制器
- (void)setUpChildViewControllers{
    
    //首页
    [self addChildVc:[SCHomeController new] title:@"首页" image:@"home_n" selectedImage:@"home_s"];
    //积分商城
    [self addChildVc:[SCIntegralMallController new] title:@"福豆商城" image:@"jfsc_n" selectedImage:@"jfsc_s"];
    //购物车
    [self addChildVc:[SCShopCartController new] title:@"购物车" image:@"gwc_n" selectedImage:@"gwc_s"];
    //我的
    [self addChildVc:[SCMineController new] title:@"我的" image:@"me_n" selectedImage:@"me_s"];
}
- (void)addCustomTabBar{
    SCTabBar *customTabBar = [[SCTabBar alloc] init];
    // 更换系统自带的tabbar
    [self setValue:customTabBar forKeyPath:@"tabBar"];
}
#pragma mark - 添加子控制器
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [UIImage imageWithOriginal:selectedImage];
    
    SCNavigationController *nav = [[SCNavigationController alloc]initWithRootViewController:childVc];
    [self addChildViewController:nav];
}
#pragma mark - 设置tabBar的一些属性
- (void)setupTabBarItem{
    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB(140, 140, 140), NSFontAttributeName:SCFont(11)} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB(231, 157, 58)} forState:UIControlStateSelected];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([viewController.tabBarItem.title isEqualToString:@"购物车"] || [viewController.tabBarItem.title isEqualToString:@"我的"]) {
        if (Token == nil) {
            [MyTool loginWithVc:self];
            return NO;
        }
        return YES;
    }
    return YES;
}

@end
